/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.pdx.cs.guavadata.query;

import edu.pdx.cs.guavadata.Channel;
import edu.pdx.cs.guavadata.Transform;

/**
 *
 * @author VuTran
 */
public class Check extends Operator {

    // DDL statement for a generic check constraint (Tier 3 foreign key)
    Operator query1;
    Operator query2;

    public Operator getQuery1() {
        return query1;
    }

    public void setQuery1(Operator query1) {
        this.query1 = query1;
    }

    public Operator getQuery2() {
        return query2;
    }

    public void setQuery2(Operator query2) {
        this.query2 = query2;
    }

    public Check(Operator query1, Operator query2) {
        try {
            if (!(query1.isQuery()) || !(query2.isQuery()))
                throw new Exception("Cannot perform a check constraint on objects that are not queries.");
        } catch (Exception e) {
            System.err.println("ERROR: " + e.toString());
        }

        this.query1 = query1;
        this.query2 = query2;
    }

    @Override
    public boolean isQuery() {
        return false;
    }

    @Override
    public SqlText visit(Provider qv) {
        return qv.transform(this);
    }

    @Override
    public void visit(Channel c, String table) {
        c.mapAliases(this, table);
    }

    @Override
    public void visit(Transform t, Command c) {
        t.expand(this, c);
    }

    @Override
    public void notifyChange(Operator old, Operator newOp) {
        if (this.query1 == old)
            this.query1 = newOp;
        if (this.query2 == old)
            this.query2 = newOp;
    }

    @Override
    public Operator clone() {
        return new Check(query1.clone(), query2.clone());
    }

}
